package com.wyr.common.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 分布式redis锁配置
 *
 * @author wangyiren
 * @version 1.0
 * &#064;title RedissonConfig
 * &#064;description
 * &#064;date 2025/03/06 14:44
 */
@Component
public class RedissonConfig {

    @Value("${spring.data.redis.host}")
    private String host;

    @Value("${spring.data.redis.port}")
    private String port;

    @Value("${spring.data.redis.password}")
    private String password;

    @Value("${spring.data.redis.database}")
    private String database;

    /**
     * 单机模式
     */
    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + host + ":" + port)
                .setPassword(password)
                .setDatabase(Integer.parseInt(database));
        return Redisson.create(config);
    }

    /** Redis主从模式 */
    /*
    @Bean
    public RedissonClient masterSlaveModeRedisson() {
        Config config = new Config();
        // 设置主从模式下的服务器地址
        config.useMasterSlaveServers()
                 // 主服务器IP
                .setMasterAddress("redis://192.168.230.131:6379")
                // 从服务器IP
                .addSlaveAddress("redis://192.168.230.132:6379", "redis://192.168.230.133:6379")
                .setPassword("123456");
        return Redisson.create(config);
    }
    */

    /** Redis哨兵模式 */
    /*
    @Bean
    public RedissonClient sentinelModeRedisson() {
        Config config = new Config();
        // 设置哨兵模式下的服务器地址
        config.useSentinelServers()
                .setMasterName("myMaster")
                .addSentinelAddress(
                        "redis://192.168.230.131:6379",
                        "redis://192.168.230.132:6379",
                        "redis://192.168.230.133:6379")
                .setPassword("123456");
        return Redisson.create(config);
    }
    */

    /** Redis集群模式 */
    /*
    @Bean
    public RedissonClient clusterModeRedisson() {
        Config config = new Config();
        // 设置集群模式下的服务器地址
        config.useClusterServers()
                .addNodeAddress(
                        "redis://192.168.230.131:6379",
                        "redis://192.168.230.132:6379",
                        "redis://192.168.230.133:6379")
                .setPassword("123456")
                // 设置集群状态扫描间隔时间（单位为毫秒）
                .setScanInterval(2000);
        return Redisson.create(config);
    }
    */
}
